import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shop/config/config.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/life/Index.dart';
import 'package:shop/shop/life/Login.dart';

class Regist extends StatefulWidget {
  Regist({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _Regist createState() => _Regist();
}

class _Regist extends State<Regist> {
  TextEditingController _unameController =
      new TextEditingController(); // 用户用户名从这里获取
  TextEditingController _telController =
      new TextEditingController(); // 用户用户名从这里获取
  TextEditingController _pwdController = new TextEditingController();
  TextEditingController _pwdController2 = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("欢迎注册"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              child: TextFormField(
                obscureText: false,
                controller: _unameController,
                decoration: InputDecoration(
                  hintText: "用户名至少3位字符",
                  icon: Icon(Icons.perm_identity),
                  labelText: "请输入用户名",
                ),
                validator: (val) {
                  if (val.trim().length < 1) return "用户名不能为空";
                  return null;
                },
                onSaved: (str) {
                  _unameController.text = str; //str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                obscureText: false,
                controller: _telController,
                decoration: InputDecoration(
                  hintText: "请输入手机号码",
                  icon: Icon(Icons.call),
                  labelText: "请输入手机号码",
                ),
                validator: (val) {
                  if (val.trim().length < 1) return "手机号码不能为空";
                  return null;
                },
                onSaved: (str) {
                  _telController.text = str; //str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                // maxLines: 1,
                // maxLength: 12,
                obscureText: true,
                controller: _pwdController,
                decoration: InputDecoration(
                  hintText: "密码至少6位",
                  icon: Icon(Icons.https),
                  labelText: "请输入密码",
                ),
                validator: (val) {
                  if (val.trim().length < 1) return "密码不能为空";
                  return null;
                },
                onSaved: (str) {
                  _pwdController.text = str; //str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                // maxLines: 1,
                // maxLength: 12,
                obscureText: true,
                controller: _pwdController2,
                decoration: InputDecoration(
                  hintText: "密码至少6位",
                  icon: Icon(Icons.https),
                  labelText: "请再次输入密码",
                ),
                validator: (val) {
                  if (val.trim().length < 1) return "密码不能为空";
                  return null;
                },
                onSaved: (str) {
                  _pwdController2.text = str; //str为文本的内容值
                },
              ),
            ),
            Container(
              child: RaisedButton(
                shape: RoundedRectangleBorder(),
                onPressed: () async {
                  if (_unameController.text.trim().length < 3) {
                    Fluttertoast.showToast(
                        msg: "用户名不能为空",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0);
                    return;
                  }
                  if (_telController.text.trim().length < 3) {
                    Fluttertoast.showToast(
                        msg: "手机号码不能为空",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0);
                    return;
                  }
                  if (_pwdController.text.trim().length < 6) {
                    Fluttertoast.showToast(
                        msg: "密码位数不够",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0);
                    return;
                  }
                  if (_pwdController.text.trim() !=
                      _pwdController2.text.trim()) {
                    Fluttertoast.showToast(
                        msg: "两次密码不一致",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0);
                    return;
                  }
                  print(_pwdController.text.trim());
                  Response response = await Dio().post(Config.getInstance().getHost()+"app/user/regist",
                      // "http://xllm.szghrj.com:8082/app/user/regist",
                      data: {
                        "userName": _unameController.text.trim(),
                        "userPhone": _telController.text.trim(),
                        "userPassword": _pwdController.text.trim()
                      });
                  var x = json.decode(response.toString());
                  if (x['code'] == 200) {
                    Fluttertoast.showToast(
                        msg: "注册成功",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0);
                    MyRoute.push(Login());
                    // Navigator.pushReplacement(
                    //     context,
                    //     MaterialPageRoute(
                    //         builder:(context)=>Login()
                    //     )
                    // );
                  }
                },
                child: Text(
                  "注册",
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Text("立即登录"),
              ],
            )
          ],
        ),
      ),
    );
  }
}
